//
//  HTSubjectScreen.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/1.
//

import SwiftUI
import Refresh
import PKHUD

struct HTSubjectScreen: View {

  @EnvironmentObject private var var_store: HTAppStore
  var var_subId: String? = ""
  var var_subTitle: String? = ""
  @State private var var_selAnime: HTAnime = HTAnime()
  @State private var var_showPushToVideoDetail: Bool = false
  @State private var var_subjectList: [HTAnime] = []
  @State private var var_page = 1
  @State private var var_page_size = 10
  @State private var var_headerRefreshing: Bool = false
  @State private var var_footerRefreshing: Bool = false
  @State private var var_noMore: Bool = false

  var body: some View {
    HTNavigationBar{
      VStack(){
        NavigationLink(destination: HTVideoDetailScreen(var_id: var_selAnime.var_id, var_source: "10"), isActive: $var_showPushToVideoDetail) {
          EmptyView()
        }

        ScrollView {
          RefreshHeader(refreshing: $var_headerRefreshing, action: {
            self.ht_loadData()
          }) { progress in
            HTActivityIndicator(var_style: .medium)
          }

          // 列表内容
          VStack(alignment: .leading, spacing: 0){
            ForEach(0..<var_subjectList.count, id: \.self) { index in
              Button {
                var_selAnime = var_subjectList[index]
                var_showPushToVideoDetail = true
                HTNetworkManager().ht_requestForBuried(.htEnumMovielistCl(var_movielist_id: var_subId!, var_movie_id: var_selAnime.var_id, var_movie_name: var_selAnime.var_title ?? ""))
              } label: {
                HTSubjectCell(var_anime: var_subjectList[index])
              }
            }
          }
          .frame(maxWidth: .infinity, maxHeight: .infinity)

          if var_subjectList.count > 0 {
            RefreshFooter(refreshing: $var_footerRefreshing, action: {
              self.ht_loadMore()
            }) {
              if var_noMore {
                Text("No more data !")
                  .foregroundColor(.secondary)
                  .padding()
              } else {
                HTActivityIndicator(var_style: .medium)
                  .padding()
              }
            }
            .noMore(var_noMore)
            .preload(offset: 50)
          }
        }
        .enableRefresh()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color(hex: "#f5f5f5"))
      }
      .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
    .ht_title(title: var_subTitle!)
    .onAppear{
      if (var_subjectList.count == 0) {
        ht_loadData()
      }
      HTNetworkManager().ht_requestForBuried(.htEnumMovielistSh(var_movielist_id: var_subId!))
    }
  }

  func ht_loadData() {
    var_subjectList = []
    var_page = 1
    var_store.dispatch(HTEnumAnimeAction.ht_subjectList(params: ["id": var_subId!, "page": "\(var_page)", "page_size": "\(var_page_size)"], andCallback: { var_result in
      var_subjectList = var_result ?? []
      var_headerRefreshing = false
      var_noMore = false
    }))
  }

  func ht_loadMore() {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
      var_page += 1
      var_store.dispatch(HTEnumAnimeAction.ht_subjectList(params: ["id": var_subId!, "page": "\(var_page)", "page_size": "\(var_page_size)"], andCallback: { var_result in
        var_footerRefreshing = false
        if (var_result != nil && var_result!.count == 0) {
          var_noMore = true
        } else {
          var_noMore = false
          var_subjectList = var_subjectList + (var_result ?? [])
        }
      }))
    }
  }
}
